<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    > 
    <el-form-item label="报废原因" prop="scrapReason">
      <el-input v-model="formData.scrapReason" placeholder="请输入报废原因" />
    </el-form-item>
      
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as AssetApi from '@/api/eam/asset'
import * as CatalogApi from '@/api/eam/catalog'
import * as DeptApi from '@/api/system/dept'
import * as UserApi from '@/api/system/user'
import * as ManufacturerFormApi from '@/api/eam/manufacturer'
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'

const { t } = useI18n() // 国际化
const activeName = ref('basic')
const message = useMessage() // 消息弹窗
import { useUserStore } from '@/store/modules/user'
import { defaultProps, handleTree } from '@/utils/tree'
import dayjs from 'dayjs'
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData: any = ref({
  id: undefined,
 
 
  
  
  

})

/** 根据选择的部门筛选员工清单 */
// const userListByDeptId = computed(() =>
//   formData.deptId
//     ? userList.value.filter((u: any) => u.deptId === formData.deptId)
//     : []
// )

const deptList = ref<Tree[]>([]) // 部门树形结构
const userList = ref<UserApi.UserVO[]>([]) // 全量用户清单
const CatalogList: any = ref([]) 
// const ManufacturerList: any = ref([]) 

const formRules = reactive({
  scrapReason: [
      { required: true, message: '请输入报废原因', trigger: 'change' }
    ],
})
const formRef = ref() // 表单 Ref
/** 初始化 */
onMounted(async () => {
  console.log('AssetForm.vue')
  deptList.value = handleTree(await DeptApi.getDeptPage({}))
  userList.value = handleTree(await UserApi.getSimpleUserList())
   let res: any = await CatalogApi.getCatalogPage({
    pageNo: 1,
    pageSize: 10000
  })
  res = res.list
  CatalogList.value = handleTree(res)

  //  let res2: any = await ManufacturerFormApi.getManufacturerPage({
  //   pageNo: 1,
  //   pageSize: 100
  // })
  // res2 = res2.list
  // ManufacturerList.value = res2
})

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '填写报废原因'
  formType.value = type
  formData.value = {
    id
  }
  // resetForm()

}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as AssetApi.AssetVO
    // data.maintenanceEndDate = data.maintenanceEndDate ? dayjs(data.maintenanceEndDate).format('YYYY-MM-DD') : "";
    // data.maintenanceStartDate =  data.maintenanceStartDate ? dayjs(data.maintenanceStartDate).format('YYYY-MM-DD') : '';
    if (formType.value === 'create') {
      await AssetApi.createAsset(data)
      message.success(t('common.createSuccess'))
    } else {
      await AssetApi.updateAsset({
        ...data,
        assetStatus: "4"
      })
      message.success('报废成功')
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    
   
    
   
    monthDepreciationPrice: undefined,
    entryTime: undefined,
    financialNotes: undefined,
    equipmentCode: undefined,
    equipmentStatus: undefined,
    equipmentIp: undefined,
    manageIp: undefined,
    equipmentCpu: undefined,
    equipmentMemory: undefined,
    equipmentLabel: undefined,
    equipmentConf: undefined,
    equipmentEnvironmentCode: undefined,
    equipmentSerialNumber: undefined,
    rackId: undefined,
    rackUpNumber: undefined,
    rackDownNumber: undefined,
    label: undefined,
    label3: undefined,
    label5: undefined,
    label4: undefined,
    label2: undefined,
    billId: undefined,
    lastInspectTime: undefined,
    longitude: undefined,
    dimension: undefined,
    internalControlLabel: undefined,
    collectionId: undefined,
    borrowId: undefined,
    scrapId: undefined,
    handleId: undefined,
    originatorId: undefined,
    chsType: undefined,
    chsStatus: undefined,
    chsVersion: undefined,
    changeInstanceId: undefined,
    summary: undefined,
    latestApproverId: undefined,
    latestApproverName: undefined,
    nextApproverIds: undefined,
    nextApproverNames: undefined,
    approvalOpinion: undefined,
    assetSelectedData: undefined,
  }
  formRef.value?.resetFields()
}
</script>